'use strict';

const Controller = require('egg').Controller;

const moment = require('moment');

class ScoreController extends Controller {
    //添加成绩
    async create() {
        //1.接收参数  容错  添加
        let {ctx,service} = this;
        let {username,skill,theory,num} = ctx.request.body;
        if(username && (0<=skill<=100) && (0<=theory<=100)){
            let time = moment(new Date()).format('YYYY-MM-DD'); //2020-11-20
            console.log(time);
            let target = await service.score.select(time,num);
            if(target.length){
                ctx.body = {
                    code:3,
                    msg:'已录入'
                }
            }else{
                try{
                    await service.score.add(username,skill,theory,num,time);
                    ctx.body = {
                        code:1,
                        msg:'录入成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //修改成绩
    async update() {
        //1.接收参数  容错  添加
        //2.id
        let {ctx,service} = this;
        let {skill,theory} = ctx.request.body;
        let {id} = ctx.params;
        if((0<=skill<=100) && (0<=theory<=100) && id){
            try{
                await service.score.update(skill,theory,id);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //删除成绩
    async destroy(){
        let {ctx,service} = this;
        let {id} = ctx.params;
        if(id){
            try{
                await service.score.del(id);
                ctx.body = {
                    code:1,
                    msg:'删除成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //查询   只查今天的
    async index(){
        let {ctx,service} = this;
        //获取今天的时间
        let timer = moment(new Date()).format('YYYY-MM-DD');
        let list = await service.score.index(timer);
        ctx.body = {
            code:1,
            data:list
        }
    }

    //获取今天未录入成绩的人员名单
    async notScore(){
        let {ctx,service} = this;
        //获取今天的时间
        let timer = moment(new Date()).format('YYYY-MM-DD');
        let notScore = await service.score.notScore(timer);
        ctx.body = {
            code:1,
            data:notScore
        }

    }

    //搜索
    async search(){
        let {ctx,service} = this;

        //获取关键字
        let {searchKey} = ctx.query;
        let timer = moment(new Date()).format('YYYY-MM-DD');

        if(searchKey){
            let list = await service.score.search(searchKey,timer);
            ctx.body = {
                code:1,
                data:list
            }
        }else{
            let list = await service.score.index(timer);
            ctx.body = {
                code:1,
                data:list
            }
        }
    }

}

module.exports = ScoreController;
